package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.util.DateUtil;
import com.yunxi.hubToBiz.pojo.HubProducePlanVO;
import com.yunxi.hubToBiz.service.ProducePlanBroadcastDubboService;
import com.yunxi.model.otd.OtdOrderSo;
import com.yunxi.model.sto.VehicleOffline;
import com.yunxi.service.localService.otd.OtdOrderSoService;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 生产计划广播接口
 *
 * @author XUQI
 */
public class ProducePlanBroadcastDubboServiceImpl implements ProducePlanBroadcastDubboService {

    private static final Logger logger = LoggerFactory.getLogger(ProducePlanBroadcastDubboServiceImpl.class);


    @Autowired
    @Qualifier("vehicleOfflineServiceImpl")
    VehicleOfflineService carService;

    @Autowired
    @Qualifier("otdOrderSoServiceImpl")
    OtdOrderSoService soService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(List<HubProducePlanVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            logger.debug("待处理数据为空");
        }

        for (HubProducePlanVO data : list) {
            if (StringUtils.isEmpty(data.getZobl1())) {
                logger.debug("生产计划编号不能为空");
                continue;
            }
            //查询车辆信息
            VehicleOffline car = carService.selectCarByPlanNo(data.getZobl1());
            if (car == null) {
                logger.debug("未找到车辆下线信息，PlanNo" + data.getZobl1());
                continue;
            }
            //查询下发生产计划接口数据
            LambdaQueryWrapper<OtdOrderSo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OtdOrderSo::getPlanNo, data.getZobl1());
            OtdOrderSo orderSo = soService.getOne(queryWrapper);

            if (orderSo == null) {
                logger.debug("未找到生产计划，PlanNo" + data.getZobl1());
                continue;
            }
            // 广播时间
            if (orderSo.getBroadcastDate() == null) {
                orderSo.setSapOrderNo(data.getOrdno());
                orderSo.setBroadcastDate(new Date());
                orderSo.setUpdateBy("SAP广播");
                orderSo.setUpdateTime(new Date());
                soService.updateById(orderSo);
            }

            car.setProductCode(orderSo.getProductCode());
            car.setCustomPackCode(orderSo.getCustomPackCode());
            // 预计收车时间
            car.setExpectedOfflineTime(DateUtil.parse(data.getZdatee(), "yyyyMMddHHmmss"));
            // 预计进焊装时间
            car.setExpectedInWeldingTime(DateUtil.parse(data.getZdateb(), "yyyyMMddHHmmss"));
            // 预计进涂装时间
            car.setExpectedInPaintTime(DateUtil.parse(data.getZdatep(), "yyyyMMddHHmmss"));
            // 预计进总装时间
            car.setExpectedInFinalAssemblyTime(DateUtil.parse(data.getZdatea(), "yyyyMMddHHmmss"));
            carService.updateById(car);
        }
    }
}
